package gae.colopl.http;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import gae.colopl.http.bean.ItemWHData;
import gae.colopl.http.bean.LinkData;
import gae.colopl.http.bean.PresentOmiyageData;
import gae.colopl.http.bean.StateData;
import gae.colopl.http.bean.TreasureBoxData;
import gae.colopl.http.bean.TreasureBoxTopData;
import gae.colopl.http.bean.TresureBoxSummaryData;
import gae.colopl.http.request.ColoplPCLoginManager;
import gae.colopl.http.request.ItemSetWHActionRequest;
import gae.colopl.http.request.ItemWHRequest;
import gae.colopl.http.request.LinkRequest;
import gae.colopl.http.request.PresentOmiyageDoRequest;
import gae.colopl.http.request.PresentOmiyageRequest;
import gae.colopl.http.request.StateRequest;
import gae.colopl.http.request.TreasureBoxRequest;
import gae.colopl.http.request.TresureBoxTopRequest;
import gae.colopl.http.user.Session;
import gae.colopl.http.user.SessionManager;
import gae.colopl.http.user.SessionManagerLocal;

public class ColoplRequestManager {
    
    private static ColoplRequestManager manager = new ColoplRequestManager();
    
    private SessionManager sessionManager = null;
    
    
    private ColoplRequestManager() {
        sessionManager = new SessionManagerLocal();
    }
    
    public static ColoplRequestManager getInstance() {
        return ColoplRequestManager.manager;
    }
    
    public Session requestLogin(String username, String password) throws ColoplLoginException {
        
        Session userSession = sessionManager.get(username);
        try {
            if (userSession == null) {
                userSession = sessionManager.createUser(username, password);
                ColoplPCLoginManager.getInstance().login(userSession);
            }
        } catch (ColoplLoginException e) {
            throw e;
        } finally {
            sessionManager.put(userSession.getUsername(), userSession);
        }
        return userSession;
    }
    
    public StateData requestStatusDeail(String username, String password, int serial) throws ColoplRequestException {
        Session session = sessionManager.get(username);
        if (session == null) {
            session = sessionManager.createUser(username, password);
        }
        
        StateData data = null;
        try {
            Map<String, String> param = new HashMap<String, String>(2);
            param.put("serial", String.valueOf(serial));
            StateRequest req = StateRequest.getInstance();
            data = req.execute(session, param);
        } catch (ColoplRequestException e) {
            throw e;
        } finally {
            sessionManager.put(session.getUsername(), session);
        }
        return data;
    }

    public TreasureBoxTopData requestTresureBoxTop(String username, String password) throws ColoplRequestException {
        Session session = sessionManager.get(username);
        if (session == null) {
            session = sessionManager.createUser(username, password);
        }
        
        TreasureBoxTopData data = null;
        try {
            TresureBoxTopRequest req = TresureBoxTopRequest.getInstance();
            data = req.execute(session, null);
        } catch (ColoplRequestException e) {
            throw e;
        } finally {
            sessionManager.put(session.getUsername(), session);
        }
        return data;
    }
    
    public TreasureBoxData requestTresureBox(String username, String password, String boxId, int point) throws ColoplRequestException {
        Session session = sessionManager.get(username);
        if (session == null) {
            session = sessionManager.createUser(username, password);
        }
        
        TreasureBoxData data = null;
        try {
            TreasureBoxRequest req = TreasureBoxRequest.getInstance();
            Map<String, String> param = new HashMap<String, String>(2);
            param.put("boxID", boxId);
            param.put("point", String.valueOf(point));
            data = req.execute(session, param);
        } catch (ColoplRequestException e) {
            throw e;
        } finally {
            sessionManager.put(session.getUsername(), session);
        }
        return data;
    }
    
    public ItemWHData requestItemWH(String username, String password, int point) throws ColoplRequestException {
        Session session = sessionManager.get(username);
        if (session == null) {
            session = sessionManager.createUser(username, password);
        }
        
        ItemWHData data = null;
        try {
            ItemWHRequest req = ItemWHRequest.getInstance();
            Map<String, String> param = new HashMap<String, String>(2);
            param.put("point", String.valueOf(point));
            data = req.execute(session, param);
        } catch (ColoplRequestException e) {
            throw e;
        } finally {
            sessionManager.put(session.getUsername(), session);
        }
        return data;
    }
    
    public PresentOmiyageData requestPresentOmiyage(String username, String password, int toID, int area) throws ColoplRequestException {
        Session session = sessionManager.get(username);
        if (session == null) {
            session = sessionManager.createUser(username, password);
        }
        
        PresentOmiyageData data = null;
        try {
            PresentOmiyageRequest req = PresentOmiyageRequest.getInstance();
            Map<String, String> param = new HashMap<String, String>(2);
            param.put("toID", String.valueOf(toID));
            param.put("area", String.valueOf(area));
            data = req.execute(session, param);
        } catch (ColoplRequestException e) {
            throw e;
        } finally {
            sessionManager.put(session.getUsername(), session);
        }
        return data;
    }
    
    public Boolean requestPresentOmiyageDo(String username, String password, int toID, int area, int omiyageID, int num) throws ColoplRequestException {
        Session session = sessionManager.get(username);
        if (session == null) {
            session = sessionManager.createUser(username, password);
        }
        
        Boolean data = null;
        try {
            PresentOmiyageDoRequest req = PresentOmiyageDoRequest.getInstance();
            Map<String, String> param = new HashMap<String, String>(4);
            param.put("toID", String.valueOf(toID));
            param.put("area", String.valueOf(area));
            param.put("omiyageID", String.valueOf(omiyageID));
            param.put("num", String.valueOf(num));
            data = req.execute(session, param);
        } catch (ColoplRequestException e) {
            throw e;
        } finally {
            sessionManager.put(session.getUsername(), session);
        }
        return data;
    }
    
    public LinkData requestLink(String username, String password) throws ColoplRequestException {
        Session session = sessionManager.get(username);
        if (session == null) {
            session = sessionManager.createUser(username, password);
        }
        
        LinkData data = null;
        try {
            LinkRequest req = LinkRequest.getInstance();
            data = req.execute(session, null);
        } catch (ColoplRequestException e) {
            throw e;
        } finally {
            sessionManager.put(session.getUsername(), session);
        }
        return data;
    }
    
    public Boolean requestItemSetAction(String username, String password, int autoID, int priority, int price, String msg) throws ColoplRequestException {
        Session session = sessionManager.get(username);
        if (session == null) {
            session = sessionManager.createUser(username, password);
        }
        
        Boolean data = null;
        try {
            ItemSetWHActionRequest req = ItemSetWHActionRequest.getInstance();
            Map<String, String> param = new HashMap<String, String>(2);
            param.put("autoID", String.valueOf(autoID));
            param.put("priority", String.valueOf(priority));
            param.put("price", String.valueOf(price));
            param.put("msg", String.valueOf(msg));
            data = req.execute(session, param);
        } catch (ColoplRequestException e) {
            throw e;
        } finally {
            sessionManager.put(session.getUsername(), session);
        }
        return data;
    }
    
    public SessionManager getSessionManager() {
        return sessionManager;
    }

    public void setSessionManager(SessionManager userManager) {
        this.sessionManager = userManager;
    }
    
}
